package eszkola;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class SortujWgNazwiskaS implements Comparator<Student> {

	public int compare(Student o1, Student o2) {
		return o1.getSurname().compareTo(o2.getSurname());
	}

}

class SortujWgNazwiskaT implements Comparator<Teacher> {

	public int compare(Teacher o1, Teacher o2) {
		return o1.getSurname().compareTo(o2.getSurname());
	}

}

public class Eszkola {

	public static void main(String[] args) throws IOException {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(
					"dziennik.txt"));
			BufferedReader reader1 = new BufferedReader(new FileReader(
					"dziennik.txt"));
			BufferedReader teacher = new BufferedReader(new FileReader(
					"teacher.txt"));
			BufferedReader teacher1 = new BufferedReader(new FileReader(
					"teacher.txt"));
			String line = null;
			Integer idx = 0;
			Integer idxt = 0;
			while ((line = reader.readLine()) != null) {
				idx++;
			}
			reader.close();
			System.out.println(idx);
			String teacherLine = null;
			while ((teacherLine = teacher.readLine()) != null) {
				idxt++;
			}
			System.out.println("teacher " + idxt);
			Teacher[] teachers = new Teacher[idxt];
			final Student[] students = new Student[idx];
			final ArrayList<Student> list = new ArrayList<Student>();
			ArrayList<Teacher> listT = new ArrayList<Teacher>();
			Integer idx1 = 0;
			Integer idx2 = 0;
			String line1 = null;
			String teacherLine1 = null;

			while ((teacherLine1 = teacher1.readLine()) != null) {
				String t[] = teacherLine1.split(",");
				teachers[idx2] = new Teacher(new BigDecimal(t[0]), t[1], t[2],
						t[3]);
				listT.add(teachers[idx2]);
				System.out.println(teachers[idx2]);
				idx2++;
			}
			System.out.println("idx2 " + idx2);
			while ((line1 = reader1.readLine()) != null) {
				String u[] = line1.split(",");
				students[idx1] = new Student(new BigDecimal(u[0]), u[1], u[2],
						Integer.parseInt(u[3]), Integer.parseInt(u[4]),
						Integer.parseInt(u[5]), Integer.parseInt(u[6]),
						Integer.parseInt(u[7]), Integer.parseInt(u[8]),
						Integer.parseInt(u[9]), Integer.parseInt(u[10]),
						Integer.parseInt(u[11]), Integer.parseInt(u[12]),
						Integer.parseInt(u[13]), Integer.parseInt(u[14]),
						Integer.parseInt(u[15]), Integer.parseInt(u[16]),
						Integer.parseInt(u[17]), Integer.parseInt(u[18]),
						Integer.parseInt(u[19]), Integer.parseInt(u[20]),
						Integer.parseInt(u[21]), Integer.parseInt(u[22]),
						Integer.parseInt(u[23]), Integer.parseInt(u[24]),
						Integer.parseInt(u[25]), Integer.parseInt(u[26]),
						Integer.parseInt(u[27]), Integer.parseInt(u[28]),
						Integer.parseInt(u[29]), Integer.parseInt(u[30]),
						Integer.parseInt(u[31]), Integer.parseInt(u[32]),
						Integer.parseInt(u[33]));
				list.add(students[idx1]);
				idx1++;
			}
			reader1.close();
			students[1].setPolski1(3);
			Collections.sort(listT, new SortujWgNazwiskaT());
			Collections.sort(list, new SortujWgNazwiskaS());
			for (Teacher teacher2 : listT) {
				System.out.println("po sortowaniu: " + teacher2);
			}
			// System.out.println(teachers[1]);

			for (Student student : list) {
				System.out.println("po sortowaniu: " + student);
			}

			JFrame jFrame = new JFrame();
			JPanel jPanel = new JPanel();
			JButton jButton1 = new JButton("1");
			JButton jButton2 = new JButton("2");
			final JLabel jLabel = new JLabel();
			final String ww = "2";
			final JTextField jTextField = new JTextField(ww, 10);
			jFrame.add(jPanel);
			jPanel.add(jTextField);
			jPanel.add(jButton1);
			jPanel.add(jButton2);
			ActionListener okno = new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					JFrame jFrame2 = new JFrame("okienko");
					JPanel jPanel2 = new JPanel();
					JButton jButton = new JButton();
					jFrame2.add(jPanel2);
					jFrame2.add(jButton);

					JLabel jLabel2 = new JLabel();
					jFrame2.add(jLabel2);
					jFrame2.setSize(400, 200);
					jFrame2.setVisible(true);
					jLabel2.setText("wybra�e� klas�: "+jTextField.getText());

					BigDecimal qq = new BigDecimal(1001);
					for (Student student : list) {
						Integer okn = Integer.parseInt(jTextField.getText());
						
//						System.out.println("-------------------okn " + okn);
						if (student.getKlasa() == okn) {
							BigDecimal war = student.getUid();
							BigDecimal wynik = (war.subtract(qq));
							hejka(wynik);
//							System.out.println("wynik " + wynik);
							System.out.println(student.getUid()
									+ "po sortowaniu11: " + qq + " ---> "
									+ student);
							
						}
					}
					// System.out.println(qq);
					// System.exit(0);
				}

			};
			jButton1.addActionListener(okno);
			ActionListener button2 = new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					BigDecimal wynik2 = new BigDecimal(2);
					hejka(wynik2);
					
				}
			};
			jButton2.addActionListener(button2);

			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jFrame.setSize(400, 200);
			jFrame.setVisible(true);

			BufferedWriter writerStudent = new BufferedWriter(new FileWriter(
					"dziennik.txt"));
			for (Student student11 : students) {
				System.out.println("nie posortowane: " + student11);
				writerStudent.write(student11
						+ System.getProperty("line.separator"));
			}
			writerStudent.flush();
		} catch (FileNotFoundException file) {
			System.out.println("nie znaleziono pliku " + file);
			// e.printStackTrace();
		}

	}

	protected static void hejka(BigDecimal wynik2) {
		System.out.println(wynik2);
		// JFrame jFrame = new JFrame("My First Swing App");

		// jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// JPanel jPanel = new JPanel();
		// jFrame.add(jPanel);
		// JLabel jLabel = new JLabel("Enter Age");
		// jPanel.add(jLabel);
		// JTextField jTextField = new JTextField(10);
		// jPanel.add(jTextField);
		// JButton jButton = new JButton("ok");
		// GridLayout gridLayout = new GridLayout();
		// gridLayout.setColumns(2);
		// jPanel.add(jButton);
		// jFrame.setSize(400, 200);
		// jFrame.setVisible(true);
	}
}
